<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
  <th:block th:include="include :: header('项目合同列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
  <div class="row">
    <div class="col-sm-12 search-collapse">
      <form id="formId">
        <div class="select-list">
          <ul>
            <li>
              合同名称：<input type="text" name="contractName"/>
            </li>
            <li>
              <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
              <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
            </li>
          </ul>
        </div>
      </form>
    </div>
    <div class="btn-group-sm" id="toolbar" role="group">
      <a class="btn btn-danger btn-del disabled" onclick="$.operate.removeAll()" shiro:hasPermission="project:projectContract:remove">
        <i class="fa fa-remove"></i> 删除
      </a>
      <a class="btn btn-success" onclick="uploadTpl()">
        <i class="fa fa-upload"></i> 上传合同模板
      </a>
      <a class="btn btn-info" onclick="downloadTpl('service.contract.tpl')">
        <i class="fa fa-download"></i> 下载服务合同模板
      </a>
      <a class="btn btn-info" onclick="downloadTpl('sale.contract.tpl')">
        <i class="fa fa-download"></i> 下载销售合同模板
      </a>
    </div>
    <div class="col-sm-12 select-table table-striped">
      <table id="bootstrap-table" data-mobile-responsive="true"></table>
    </div>
  </div>
</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
  const editFlag = [[${@permission.hasPermi('project:projectContract:edit')}]];
  const removeFlag = [[${@permission.hasPermi('project:projectContract:remove')}]];
  const progressDatas = [[${@dict.getType('project_progress')}]];
  const prefix = ctx + "project/projectContract";

  $(function () {
    var options = {
      url: prefix + "/list",
      createUrl: prefix + "/add",
      updateUrl: prefix + "/edit/{id}",
      removeUrl: prefix + "/remove",
      modalName: "项目合同",
      columns: [{
        checkbox: true
      },
        {
          field: 'contractId',
          title: '合同主键ID',
          visible: false
        },
        {
          field: 'projectName',
          title: '项目名称'
        },
        {
          field : 'progressCode',
          title : '项目进度',
          formatter: function(value, row, index) {
            return $.table.selectDictLabel(progressDatas, value);
          }
        },
        {
          field: 'serviceContractName',
          title: '服务合同名称'
        },
        {
          field: 'saleContractName',
          title: '销售合同名称'
        },
        {
          field: 'userName',
          title: '上传人'
        },
        {
          field: 'createTime',
          title: '上传时间'
        },
        {
          title: '操作',
          align: 'center',
          formatter: function (value, row, index) {
            var actions = [];
            // if (row.progressCode == '4' || $.common.isEmpty(row.contractId)) {
            //   actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="uploadContract(\'' + row.projectId + '\')"><i class="fa fa-edit"></i>上传合同</a> ');
            // }
            if (Number(row.progressCode) >= 4 || $.common.isEmpty(row.contractId)) {
              actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="uploadContract(\'' + row.projectId + '\')"><i class="fa fa-edit"></i>上传合同</a> ');
            }
            if ($.common.isNotEmpty(row.contractId)) {
              actions.push('<a class="btn btn-warning btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="downloadContract(\'' + row.serviceContractUrl + '\')"><i class="fa fa-edit"></i>下载服务合同</a> ');
              actions.push('<a class="btn btn-warning btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="downloadContract(\'' + row.saleContractUrl + '\')"><i class="fa fa-edit"></i>下载销售合同</a> ');
              actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.contractId + '\')"><i class="fa fa-remove"></i>删除</a>');
            }
            return actions.join('');
          }
        }]
    };
    $.table.init(options);
  });

  function uploadContract(projectId) {
    const url = prefix + '/add/' + projectId;
    $.modal.open("上传合同", url);
  }

  function downloadContract(url) {
    window.location.href = prefix + '/download?fileName='+url;
  }

  function downloadTpl(key) {
    window.location.href = prefix + '/downloadTpl?key='+key;
  }
  
  function uploadTpl() {
    layer.open({
      type: 1,
      area: ['420px', '250px'],
      fix: false,
      //不固定
      maxmin: true,
      shade: 0.3,
      title: '上传合同模板',
      content: $('#contractTpl').html(),
      btn: ['<i class="fa fa-check"></i> 上传', '<i class="fa fa-remove"></i> 取消'],
      // 弹层外区域关闭
      shadeClose: true,
      btn1: function (index, layero) {
        var serviceFile = layero.find('#serviceFile').val();
        var serviceFile2 = layero.find('#serviceFile2').val();
        var saleFile = layero.find('#saleFile').val();
        var saleFile2 = layero.find('#saleFile2').val();
        if (serviceFile == '' || (!$.common.endWith(serviceFile, '.doc') && !$.common.endWith(serviceFile, '.docx'))) {
          $.modal.msgWarning("请选择后缀为 “doc”或“docx”的服务合同模板文件。");
          return false;
        }
        if (saleFile == '' || (!$.common.endWith(saleFile, '.doc') && !$.common.endWith(saleFile, '.docx'))) {
          $.modal.msgWarning("请选择后缀为 “doc”或“docx”的销售合同模板文件。");
          return false;
        }
        var index = layer.load(2, {shade: false});
        $.modal.disable();
        var formData = new FormData();
        formData.append("serviceFile", $('#serviceFile')[0].files[0]);
        formData.append("saleFile", $('#saleFile')[0].files[0]);
        $.ajax({
          url: prefix + '/upload',
          data: formData,
          cache: false,
          contentType: false,
          processData: false,
          type: 'POST',
          success: function (result) {
            if (result.code == web_status.SUCCESS) {
              $.modal.closeAll();
              $.modal.alertSuccess(result.msg);
            } else if (result.code == web_status.WARNING) {
              layer.close(index);
              $.modal.enable();
              $.modal.alertWarning(result.msg)
            } else {
              layer.close(index);
              $.modal.enable();
              $.modal.alertError(result.msg);
            }
          }
        });
      }
    });
  }
</script>
</body>
<script id="contractTpl" type="text/template">
  <form enctype="multipart/form-data" class="form-horizontal m">
    <div class="form-group">
      <label class="col-sm-4 control-label">服务合同模板：</label>
      <div class="col-sm-8 control-label">
        <input id="serviceFile" name="serviceFile" type="file" accept="application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">
      </div>
    </div>
    <div class="form-group">
      <label class="col-sm-4 control-label">销售合同模板：</label>
      <div class="col-sm-8 control-label">
        <input id="saleFile" name="saleFile" type="file" accept="application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">
      </div>
    </div>
    <div class="col-xs-offset-1">
      <font color="red" class="pull-left mt10">
        提示：仅允许上传“doc”或“docx”格式文件！
      </font>
    </div>
  </form>
</script>
</html>